import {
    observable,
    action,
    computed
} from "mobx";
import axios from '../../utils/http'
import {
    GET_ALLYOUHUIJUAN,
    GET_ADD_YOUHUIJUAN,
    GET_ALLGOODS,
    DEL_DANZHANG_YOUHUIJUAN,
    UPDATE_ID_YOUHUIJUAN,
    GET_DANZHANG_YOUHUIJUAN,
    DEL_YOUHUIJUAN_DANZHANG,
    SEL_GOODS_TYPE,
    SEL_GOODSNAME_TYPEID
} from "../../utils/api"
import {
    message
} from 'antd'

class NewCoupons {
    @observable
    myFrom = {
        couponName: '', //优惠卷名称
        couponType: '', //优惠卷类型
        couponAmount: 0, //优惠卷金额
        couponOa: 0, //订单金额
        couponLol: 0, //订单下限
        couponRp: 0, //所需积分
        couponPk: '', //关键字
        youHuiGoods: '', //优惠商品
        GoodsId: '', //优惠商品ID
        stateDate: [], //发放日期
        effectiveDate: [], //有效日期
    }

    @observable
    myTabData = [] //表格数据

    @observable
    goodsList = [] //商品列表

    @observable//查询单张优惠卷的数据
    danZhangYouHuiJuanList = []

    @observable//发放的单条数据
    faFangData={}

    @observable//发放的from
    faFangFrom = {
        goodsCid:[]
    }
    @observable//发放的所有的type
    allTypeData = []

    @observable//查询type后的商品
    allTypeGoods = []

    //修改属性
    @action
    insertStart = (data, setData) => {
        if (data === 'couponType') this.myFrom.couponType = setData.target.value //修改type
        else if (data === 'couponName') this.myFrom.couponName = setData.target.value //修改name
        else if (data === 'couponAmount') this.myFrom.couponAmount = setData.target.value //修改优惠卷金额
        else if (data === 'stateDate') this.myFrom.stateDate = setData //修改发布日期
        else if (data === 'effectiveDate') this.myFrom.effectiveDate = setData //修改有效日期
        else if (data === 'youHuiGoods') this.myFrom.youHuiGoods = setData.target.value //修改偶会商品
        else if (data === 'keyword') this.myFrom.keyword = setData.target.value //修改关键字
        else if (data === 'couponRp') this.myFrom.couponRp = setData.target.value //修改所需积分
        else if (data === 'couponLol') this.myFrom.couponLol = setData.target.value //修改订单下限
        else if (data === 'couponOa') this.myFrom.couponOa = setData.target.value //修改订单金额
        else if (data === 'couponPk') this.myFrom.couponPk = setData.target.value //修改关键字
        else if (data === 'updateData') {
            this.myFrom = {
                couponName: setData.couponName,
                couponType: setData.couponType,
                couponAmount: setData.couponAmount,
                couponOa: setData.couponOa,
                couponLol: setData.couponLol,
                couponRp: setData.couponRp,
                couponPk: setData.couponPk,
                youHuiGoods: setData.youHuiGoods,
                GoodsId: setData.GoodsId,
                stateDate: setData.stateDate,
                effectiveDate: setData.effectiveDate,
                couponId: setData.couponId
            }
        }else if (data === 'goodsCid') this.faFangFrom.goodsCid = setData
    }

    @action
    GuanJianZiSel() { //根据关键字查询商品
        axios({
            url: GET_ALLGOODS,
            method: 'get',
            params: {
                goodsName: this.myFrom.couponPk
            }
        }).then(data => {
            if (data.data.code === 200) {
                this.goodsList = data.data.data
            }
            console.log(data)
        })
    }

    @action
    updateTabData = () => { //查询优惠卷
        // this.GuanJianZiSel()
        return axios({
            url: GET_ALLYOUHUIJUAN,
            method: 'get'
        }).then(data => {
            console.log(data.data.data)
            this.myTabData = data.data.data
            return data.data.data
        })
    }

    @action
    myMethodData() { //添加或者修改优惠卷数据
        let couponFid = this.myFrom.stateDate[0]
        let couponDoi = this.myFrom.stateDate[1]
        let couponEd = this.myFrom.effectiveDate[0]
        let couponEdf = this.myFrom.effectiveDate[1]
        if (this.myFrom.couponId) { //如果有优惠卷ID字段，那就是修改
            return axios({
                url: UPDATE_ID_YOUHUIJUAN,
                method: 'post',
                data: {
                    couponName: this.myFrom.couponName,
                    couponType: this.myFrom.couponType,
                    couponAmount: this.myFrom.couponAmount, //订单总额
                    couponOa: this.myFrom.couponOa, //订单金额
                    couponFid, //发放时间起
                    couponDoi, //发放时间始
                    couponEd, //有效日期起
                    couponEdf, //有效日期始
                    couponLol: this.myFrom.couponLol,
                    couponRp: this.myFrom.couponRp,
                    couponPk: this.myFrom.couponPk,
                    couponId: this.myFrom.couponId
                }
            }).then(data => {
                if (data.data.code === 200) {
                    message.success('修改成功');
                    return data.data.code
                }
                return data.data.code
            })
        } else { //如果没有就是添加
            return axios({
                url: GET_ADD_YOUHUIJUAN,
                method: 'post',
                data: {
                    couponName: this.myFrom.couponName,
                    couponType: this.myFrom.couponType,
                    couponAmount: this.myFrom.couponAmount, //订单总额
                    couponOa: this.myFrom.couponOa, //订单金额
                    couponFid, //发放时间起
                    couponDoi, //发放时间始
                    couponEd, //有效日期起
                    couponEdf, //有效日期始
                    couponLol: this.myFrom.couponLol,
                    couponRp: this.myFrom.couponRp,
                    couponPk: this.myFrom.couponPk,
                }
            }).then(data => {
                if (data.data.code === 200) {
                    message.success('添加成功');
                    return data.data.code
                }
                return data.data.code
            })
        }

    }

    @action
    delTabData(delTabData) { //删除表格
        return axios({
            url: DEL_DANZHANG_YOUHUIJUAN,
            method: 'get',
            params: {
                couponId: delTabData
            }
        }).then(data => {
            data.data.code === 200 ? message.success('删除成功') : message.warning('删除失败')
            return data.data.code
        })
    }

    @action
    isUpdate() {
        this.myFrom = {
            couponName: '', //优惠卷名称
            couponType: '', //优惠卷类型
            couponAmount: 0, //优惠卷金额
            couponOa: 0, //订单金额
            couponLol: 0, //订单下限
            couponRp: 0, //所需积分
            couponPk: '', //关键字
            youHuiGoods: '', //优惠商品
            GoodsId: '', //优惠商品ID
            stateDate: [], //发放日期
            effectiveDate: [], //有效日期
        }
    }

    @action
    selDanZhangYouHuiJuan(row){//查询单张优惠卷
        axios({
            url:GET_DANZHANG_YOUHUIJUAN,
            method:'get',
            params:{
                couponType:row.couponType,
                couponId:row.couponId
            }
        }).then(data=>{
            console.log(data)
            if(data.data.code === 200){
                let temp = data.data.data.map(item=>{
                    item.key = item.dcoupon_id
                    return item
                })
                this.danZhangYouHuiJuanList = temp
            }
        })
    }

    @action
    delDanZhangYouHuiJuan(id){//删除单张优惠卷或者批量删除
        return axios({
            url:DEL_YOUHUIJUAN_DANZHANG,
            method:'post',
            data:{
                dcouponIdList:id
            }
        }).then(data=>{
            if(data.data.code === 200){
                message.success('删除成功')
                return 200
            }
        })
    }

    @action//修改发放数据
    updatefaFangData(data){
        console.log(data)
        this.faFangData.couponType =2
        // this.faFangData = data
    }

    @action//查询所有发放数据类型
    selGoodsType(){
        return axios({
            url:SEL_GOODS_TYPE,
            method:'get'
        }).then(data=>{
            data.data.code === 200 ? this.allTypeData = data.data.data : message.warning('系统开小差')
            return data.data.data
        })
    }

    @action//根据发放商品类型查询商品
    selTypeGoodsList=(id)=>{
        axios({
            url:"/coupon/queryGoodsBygoodsName",
            method:'get',
            params:{
                goodsCid:id
            }
        }).then(data=>{
            console.log(data)
            if(data.data.code === 200){
                this.allTypeGoods = data.data.data.map(item=>{
                    item.key = item.goodsId
                    return item
                })
            }
        })
    }


}

export default NewCoupons